package com.iplant.wms.serviceImpl.dao.batch;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.mss.MSSMaterial;
import com.iplant.wms.service.po.batch.MSSBatchRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

@DBBaseDAO(MSSBatchRange.class)
public class MSSBatchRangeDAO extends BaseDAO {

    private static MSSBatchRangeDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(MSSBatchRangeDAO.class);

    private MSSBatchRangeDAO() {
        super("");this.Init();
    }

    public static MSSBatchRangeDAO getInstance() {
        if (Instance == null)
            Instance = new MSSBatchRangeDAO();
        return Instance;
    }

    public void MSS_DeleteBatchRange(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParwms = new HashMap<>();
            wDeleteParwms.put("RuleID", wRuleID);
            this.Delete(wDeleteParwms, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_DeleteBatchRange", ex);
        }
    }

    public MSSBatchRange MSS_QueryBatchRange(BMSEmployee wLoginUser, int wCustomerID, String wMaterialGroupFullName,
                                             int wMaterialID, OutResult<Integer> wErrorCode) {
        MSSBatchRange wResult = new MSSBatchRange();
        try {

            List<MSSBatchRange> wList =
                    this.MSS_QueryBatchRangeList(wLoginUser, wCustomerID,  wMaterialGroupFullName, -1,
                                                 Pagination.getMaxSize(), wErrorCode);

            if (wErrorCode.Result != 0 || wList == null || wList.size() <= 0) {
                return wResult;
            }

            for (MSSBatchRange mssBatchRange : wList) {

                if (mssBatchRange.MaterialID == wMaterialID) {
                    return mssBatchRange;
                }
            }

            wList.sort((o1, o2) -> o2.MaterialGroupName.length() - o1.MaterialGroupName.length());

            wResult = wList.get(0);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryBatchRange", ex);
        }
        return wResult;
    }

    public MSSBatchRange MSS_QueryBatchRange(BMSEmployee wLoginUser, int wCustomerID,
                                             String wMaterialNo, OutResult<Integer> wErrorCode) {
        MSSBatchRange wResult = new MSSBatchRange();
        try {


            MSSMaterial mssMaterial = CacheUtils.GetCacheInfo(MSSMaterial.class, -1, wMaterialNo);

            if ( mssMaterial == null || mssMaterial.ID <= 0) {
                return wResult;
            }
            wResult = this.MSS_QueryBatchRange(wLoginUser, wCustomerID, mssMaterial.MaterialFullGroup, mssMaterial.ID,
                                               wErrorCode);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryBatchRange", ex);
        }
        return wResult;
    }


    public List<MSSBatchRange> MSS_QueryBatchRangeList(
            BMSEmployee wLoginUser, int wCustomerID,
            String wMaterialGroupFullName,
            int wMaterialID,
            Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<MSSBatchRange> wMSSBatchRangeList = new ArrayList<MSSBatchRange>();
        wErrorCode.set(0);

        try {
            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("CustomerID", wCustomerID);
            wParams.put("MaterialFullGroup", wMaterialGroupFullName);
            wParams.put("MaterialID", wMaterialID);

            wMSSBatchRangeList=SelectPOList(wLoginUser,  wParams,  wPagination, wErrorCode);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryBatchRangeList", ex);
        }
        return wMSSBatchRangeList;
    }



}
